/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ihm;

import com.ihm.actions.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import com.dao.*;

public class ListeNewsEditionPanelFenetre extends JPanel 
{
    
    /*
        VARIABLES
    */

    private JPanel paneltop;
    private JPanel paneltrier;
    private JPanel panelmiddle;
    private JPanel panelbas;
    private JPanel panelbasmiddle;
    private JPanel panelbasdroite;
    private JPanel panelbasgauche;
    private JPanel panelbasmiddlehaut;
    private JPanel panelbasmiddlebas;
    private JLabel labelcarte;
    private JLabel ListeLieu;
    private JLabel lieu;
    private JLabel trier;
    private JComboBox triercombo;
    private JLabel labelrecherche;
    private JLabel nomdulieu;
    private JLabel dateajout;
    private JTextField textnom;
    private JTextField dateajoute;
    private JButton buttonrecherche;
    private ArrayList<JPanel> listpanel;
    private NewsPreparedRequete carteps;
    private JScrollPane js;
    
    /*
     *  LA Fenetre d'édition
    */
    
    public ListeNewsEditionPanelFenetre()
    {
        this.setBackground(Color.white); // Couleur de fond
	this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
	createpanel(); // Création des panels 
	createcomponent(); // Création des composants
 	addcomponent(); // Ajout des composants
        
        // Instanciations dans la fenêtre
	this.add(paneltop);
	this.add(panelmiddle);
	this.add(Box.createVerticalGlue());
	this.add(panelbas);
	
        /*
         *   Récupréation de la liste 
        */
        carteps = new NewsPreparedRequete();
        try 
        {
            carteps.afficher();
        } 
        catch (SQLException e) 
        {
            // En cas d'erreur
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,"Erreur dans la récupération des news"); 
        }
        
        listpanel = new ArrayList<>();
	createlist();
      }
      
      public void createpanel()
      {
            /* Création du panel top */
            paneltop = new JPanel();
	    paneltop.setMaximumSize(new Dimension(700,200));
	    paneltop.setSize(new Dimension(700,200));
	    paneltop.setLayout(new BoxLayout(paneltop,BoxLayout.X_AXIS));
            paneltop.setBackground(Color.white);
            
            /* Création du panel middle */
	    panelmiddle = new JPanel();
            panelmiddle.setMaximumSize(new Dimension(700,500));
            panelmiddle.setSize(new Dimension(700,500));
	    panelmiddle.setLayout(new BoxLayout(panelmiddle,BoxLayout.Y_AXIS));
            panelmiddle.setBackground(Color.white);
            
            /* Création du panel du bas */
	    panelbas = new JPanel();
	    panelbas.setMaximumSize(new Dimension(700,200));
	    panelbas.setSize(new Dimension(700,200));
	    panelbas.setLayout(new BoxLayout(panelbas,BoxLayout.X_AXIS));
            panelbas.setBackground(Color.white);
                /* Gauche*/
                panelbasgauche = new JPanel();
                panelbasgauche.setMaximumSize(new Dimension(300,200));
                panelbasgauche.setSize(new Dimension(300,200));
                panelbasgauche.setBackground(Color.white);
                /* Middle */
                panelbasmiddle = new JPanel();
                panelbasmiddle.setMaximumSize(new Dimension(500,200));
                panelbasmiddle.setSize(new Dimension(500,200));
                panelbasmiddle.setLayout(new BoxLayout(panelbasmiddle,BoxLayout.Y_AXIS));
                panelbasmiddle.setBackground(Color.white);
                /* Droite  */
                panelbasdroite = new JPanel();
                panelbasdroite.setMaximumSize(new Dimension(300,200));
                panelbasdroite.setSize(new Dimension(300,200));
                panelbasdroite.setBackground(Color.white);
                /* Haut */
                panelbasmiddlehaut = new JPanel();
                panelbasmiddlehaut.setLayout(new BoxLayout(panelbasmiddlehaut,BoxLayout.X_AXIS));
                panelbasmiddlehaut.setBackground(Color.white);
                /* bas */
                panelbasmiddlebas = new JPanel();
                panelbasmiddlebas.setLayout(new BoxLayout(panelbasmiddlebas,BoxLayout.X_AXIS));
                panelbasmiddlebas.setBackground(Color.white);
	 }
      
	 public void createcomponent()
	 {
                labelcarte = new JLabel("Liste d'édition des news");
                labelcarte.setFont(new Font("italique",Font.BOLD,20));
                labelcarte.setForeground(Color.black);
                ListeLieu = new JLabel();
                ListeLieu.setFont(new Font("italique",Font.BOLD,20));
	  }
         
	  public void addcomponent()
          {   
                paneltop.add(Box.createHorizontalStrut(20));
                paneltop.add(labelcarte);
                paneltop.add(Box.createHorizontalStrut(40));
                paneltop.add(ListeLieu);
                paneltop.add(Box.createHorizontalGlue());
	   }
          
           public void createlist()
           {
                     
                 for(int i=0;i<carteps.getDatelist().size();i++)
                 {

                         JPanel mainpanel = new JPanel();
                         JPanel panelhaut = new JPanel();
                         
                         panelhaut.setLayout(new BoxLayout(panelhaut,BoxLayout.X_AXIS));
                         
                         mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
                         mainpanel.setMaximumSize(new Dimension(700,300));
                         mainpanel.setSize(new Dimension(700,300));
                         
                         /* Boutons et Label */
                         JLabel labeltitre = new JLabel(carteps.getTitre().get(i).toString());
                         JLabel labeldate = new JLabel(carteps.getDatelist().get(i).toString());
                         JButton buttonmodifier = new JButton(new NewsConsultationAction("Modifier",carteps.getTitre().get(i).toString(),carteps.getDescription().get(i).toString()));
                         JButton buttonsupprimer = new JButton(new NewsConsultationAction("Supprimer",carteps.getTitre().get(i).toString(),carteps.getDescription().get(i).toString()));
                         
                         panelhaut.add(labeltitre);
                         panelhaut.add(Box.createHorizontalStrut(20));
                         panelhaut.add(labeldate);
                         panelhaut.add(Box.createHorizontalStrut(20));
                         panelhaut.add(buttonmodifier);
                         panelhaut.add(Box.createHorizontalStrut(10));
                         panelhaut.add(buttonsupprimer);
                         panelhaut.add(Box.createHorizontalStrut(10));
                         panelhaut.setBackground(Color.white);
                         
                         mainpanel.add(panelhaut);
                         mainpanel.add(panelbas);
                         mainpanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                         mainpanel.setBackground(Color.white);
                         
                         listpanel.add(mainpanel);
                }
                
                // Lecture de la liste
                for(int i=0;i<listpanel.size();i++)
                {
                    panelmiddle.add(listpanel.get(i));
                }
         }
}